/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpelersOverzicht.java
 *
 * Created on 13-dec-2011, 12:48:22
 */
package view;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Masterclass;
/**
 *
 * @author Nick
 */
public class MasterclassOverzicht extends javax.swing.JFrame {
    protected DefaultTableModel tableModel;
    /** Creates new form SpelersOverzicht */
    public MasterclassOverzicht() {
        initComponents();
        
        
        
    
        
        tableModel = new DefaultTableModel(){
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        };
        spelerTabel.setModel(tableModel);
        tableModel.addColumn("Masterclassid");
        tableModel.addColumn("Leraar");
        tableModel.addColumn("Datum");
        tableModel.addColumn("Kosten");
        tableModel.addColumn("Vereiste Rating");
        
        addToernooienToTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        wijzigKlas = new javax.swing.JButton();
        verwijderKlas = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        spelerTabel = new javax.swing.JTable();
        vernieuwButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Overzicht van Masterclasses");
        setLocationByPlatform(true);

        wijzigKlas.setText("Masterclass wijzigen");
        wijzigKlas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wijzigKlasActionPerformed(evt);
            }
        });

        verwijderKlas.setText("Masterclass Verwijderen");
        verwijderKlas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verwijderKlasActionPerformed(evt);
            }
        });

        spelerTabel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Code", "Adres", "Postcode", "Woonplaats", "Telefoon", "Email", "Rating"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(spelerTabel);

        vernieuwButton.setText("Vernieuw");
        vernieuwButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vernieuwButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(67, 67, 67)
                        .add(wijzigKlas)
                        .add(33, 33, 33)
                        .add(verwijderKlas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 240, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(vernieuwButton))
                    .add(layout.createSequentialGroup()
                        .add(33, 33, 33)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(8, 8, 8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(wijzigKlas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(verwijderKlas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(vernieuwButton))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void wijzigKlasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wijzigKlasActionPerformed
        new MasterclassWijzigen().setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_wijzigKlasActionPerformed

    private void verwijderKlasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verwijderKlasActionPerformed
        try {
            int masterclassid = Integer.parseInt(JOptionPane.showInputDialog(null, "Voer masterclass id in:", null));
            Masterclass masterclass = Masterclass.getMasterclass(masterclassid);
            
            if(masterclass.getMasterclassID() > 0) {
                int confirmed = JOptionPane.showConfirmDialog(null, "Weet u zeker dat u de masterclass op \""+masterclass.getDatumTijd()+"\" wilt verwijderen", "Verwijderen",JOptionPane.YES_NO_OPTION);
                if(confirmed == JOptionPane.YES_OPTION) {
                    if(masterclass.delete() == true) {
                        JOptionPane.showMessageDialog(null, "Masterclass is verwijderd", "Verwijderd",JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "Masterclass niet verwijderd", "Fout",JOptionPane.WARNING_MESSAGE);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "Masterclass id niet gevonden", "Fout",JOptionPane.WARNING_MESSAGE);

            }
        } catch(NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Masterclass id heeft onjuiste waarde", "Fout",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_verwijderKlasActionPerformed
    private void addToernooienToTable()  {
        Masterclass[] klass = Masterclass.getMasterclasses();
        for(int i=0;i<klass.length;i++) {
            Masterclass klas = klass[i];
            tableModel.addRow(new Object[]{klas.getMasterclassID(),klas.getLeraar().getNaam(),klas.getDatumTijd(),klas.getKosten(),klas.getMinimaleRating()});
        }
    }
    private void vernieuwButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vernieuwButtonActionPerformed
        // TODO add your handling code here:
        tableModel.setRowCount(0);
        addToernooienToTable();
    }//GEN-LAST:event_vernieuwButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverzicht.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverzicht.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverzicht.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverzicht.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MasterclassOverzicht().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable spelerTabel;
    private javax.swing.JButton vernieuwButton;
    private javax.swing.JButton verwijderKlas;
    private javax.swing.JButton wijzigKlas;
    // End of variables declaration//GEN-END:variables
}
